package com.aco.party.service.system;

import com.aco.party.dao.custom.ProductDao;
import com.aco.party.dao.custom.UserDao;
import com.aco.party.enums.VisitTypeEnum;
import com.aco.party.model.po.Meta;
import com.aco.party.model.po.Product;
import com.aco.party.model.po.User;
import com.aco.party.model.vo.ProductParam;
import com.aco.party.model.vo.ProductVo;
import com.aco.party.model.vo.VisitParam;
import com.aco.party.model.vo.common.PageParam;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by flying on 3/10/2016 0010.
 */
@Service
public class ProductService {
    @Resource
    private ProductDao productDao;

    @Resource MetaService metaService;

    @Resource
    private UserDao userDao;

    @Resource
    private VisitService visitService;

    public List<ProductVo> getByPage(PageParam pageParam,ProductParam param) {
        List<ProductVo> list = new ArrayList<>();
        List<Product> products = productDao.getList(pageParam,param);
        ProductVo vo;
        if (products != null) {
            for (Product product : products) {
                vo = convert2Vo(product);
                VisitParam visitParam = new VisitParam();
                visitParam.setType(VisitTypeEnum.产品.getValue());
                visitParam.setTargetId(vo.getId());
                vo.setViews(visitService.getCountByParam(visitParam).longValue());
                if(vo.getCreator() != null && vo.getCreator() != 0){
                    User user = userDao.selectByPrimaryKey(vo.getCreator());
                    if(user != null) {
                        vo.setCreatorStr(user.getNickName());
                    }
                }
                list.add(vo);
            }
        }
        return list;
    }

    public ProductVo getVoById(Long id){
        Product product = productDao.selectByPrimaryKey(id);
        ProductVo vo = convert2Vo(product);
        return vo;
    }

    private ProductVo convert2Vo(Product product){
        ProductVo vo = new ProductVo();
        if(product != null){
            BeanUtils.copyProperties(product,vo);
            if(vo.getType() != null){
                Meta meta = metaService.getByPrimaryKey(vo.getType().longValue());
                if(meta != null){
                    vo.setTypeStr(meta.getName());
                }
            }

            if(!StringUtils.isEmpty(vo.getPlatform())){
                String[] plateforms = vo.getPlatform().split("_");
                if(plateforms.length > 0){
                    List<Meta> metas = new ArrayList<>();
                    for(String plateform : plateforms){
                        Meta meta = metaService.getByPrimaryKey(Long.parseLong(plateform));
                        if(meta != null){
                            metas.add(meta);
                        }
                    }
                    StringBuilder sb = new StringBuilder();
                    for (Meta meta : metas){
                        sb.append(meta.getName() + "，");
                    }
                    vo.setPlatformStr(org.apache.commons.lang3.StringUtils.removeEnd(sb.toString(),"，"));
                }
            }
        }
        return vo;
    }

    public Integer getTotal(ProductParam param) {
        return productDao.getTotal(param);
    }

    public void update(Product product) {
        product.setUpdateTime(new Date());
        productDao.updateByPrimaryKeySelective(product);
    }

    public void add(Product product) {
        product.setCreateTime(new Date());
        productDao.insertSelective(product);
    }

    public void delete(Long id) {
        productDao.deleteByPrimaryKey(id);
    }

    public Product getByPrimaryKey(Long id) {
        return productDao.selectByPrimaryKey(id);
    }
}
